{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np #导入numpy\n",
    "import os"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0     NaN\n",
      "1    None\n",
      "2    <NA>\n",
      "dtype: object\n",
      "0    True\n",
      "1    True\n",
      "2    True\n",
      "dtype: bool\n"
     ]
    }
   ],
   "source": [
    "s = pd.Series([np.nan, None, pd.NA])\n",
    "print(s)\n",
    "print(s.isnull())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "            date  precipitation  temp_max  temp_min  wind weather\n",
      "1456  2015-12-27            NaN       NaN       NaN   NaN     NaN\n",
      "1457  2015-12-28            NaN       NaN       NaN   NaN     NaN\n",
      "1458  2015-12-29            NaN       NaN       NaN   NaN     NaN\n",
      "1459  2015-12-30            NaN       NaN       NaN   NaN     NaN\n",
      "1460  2015-12-31           20.6      12.2       5.0   3.8    rain\n"
     ]
    }
   ],
   "source": [
    "df = pd.read_csv(\"data/weather_withna.csv\")\n",
    "print(df.tail(5))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "            date  precipitation  temp_max  temp_min  wind weather\n",
      "1456  2015-12-27            NaN       NaN       NaN   NaN     NaN\n",
      "1457  2015-12-28            NaN       NaN       NaN   NaN     NaN\n",
      "1458  2015-12-29            NaN       NaN       NaN   NaN     NaN\n",
      "1459  2015-12-30            NaN       NaN       NaN   NaN     NaN\n",
      "1460         NaN           20.6      12.2       5.0   3.8    rain\n"
     ]
    }
   ],
   "source": [
    "df = pd.read_csv(\"data/weather_withna.csv\", na_values=[\"2015-12-31\"])\n",
    "print(df.tail(5))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "date               0\n",
      "precipitation    303\n",
      "temp_max         303\n",
      "temp_min         303\n",
      "wind             303\n",
      "weather          303\n",
      "dtype: int64\n"
     ]
    }
   ],
   "source": [
    "df = pd.read_csv(\"data/weather_withna.csv\")\n",
    "print(df.isnull().sum())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "            date  precipitation  temp_max  temp_min  wind weather\n",
      "1456  2015-12-27            0.0       0.0       0.0   0.0       0\n",
      "1457  2015-12-28            0.0       0.0       0.0   0.0       0\n",
      "1458  2015-12-29            0.0       0.0       0.0   0.0       0\n",
      "1459  2015-12-30            0.0       0.0       0.0   0.0       0\n",
      "1460  2015-12-31           20.6      12.2       5.0   3.8    rain\n"
     ]
    }
   ],
   "source": [
    "df = pd.read_csv(\"data/weather_withna.csv\")\n",
    "print(df.fillna(0).tail())# 使用固定值填充"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "            date  precipitation   temp_max  temp_min      wind weather\n",
      "1456  2015-12-27       3.052332  15.851468  7.877202  3.242055     NaN\n",
      "1457  2015-12-28       3.052332  15.851468  7.877202  3.242055     NaN\n",
      "1458  2015-12-29       3.052332  15.851468  7.877202  3.242055     NaN\n",
      "1459  2015-12-30       3.052332  15.851468  7.877202  3.242055     NaN\n",
      "1460  2015-12-31      20.600000  12.200000  5.000000  3.800000    rain\n"
     ]
    }
   ],
   "source": [
    "print(df.fillna(df[[\"precipitation\", \"temp_max\", \"temp_min\", \"wind\"]].mean()).tail())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "            date  precipitation  temp_max  temp_min  wind weather\n",
      "1456  2015-12-27            0.0      11.1       4.4   4.8     sun\n",
      "1457  2015-12-28            0.0      11.1       4.4   4.8     sun\n",
      "1458  2015-12-29            0.0      11.1       4.4   4.8     sun\n",
      "1459  2015-12-30            0.0      11.1       4.4   4.8     sun\n",
      "1460  2015-12-31           20.6      12.2       5.0   3.8    rain\n"
     ]
    }
   ],
   "source": [
    "print(df.ffill().tail()) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    200\n",
       "1    400\n",
       "2    600\n",
       "dtype: int64"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def func(item):\n",
    "    return item * 20\n",
    "\n",
    "s = pd.Series([10, 20, 30])\n",
    "s.apply(func)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "invalid non-printable character U+00A0 (2038230398.py, line 7)",
     "output_type": "error",
     "traceback": [
      "\u001B[1;36m  Cell \u001B[1;32mIn[17], line 7\u001B[1;36m\u001B[0m\n\u001B[1;33m    print(f(df[\"a\"], df[\"b\"]))  # ValueError\u001B[0m\n\u001B[1;37m                               ^\u001B[0m\n\u001B[1;31mSyntaxError\u001B[0m\u001B[1;31m:\u001B[0m invalid non-printable character U+00A0\n"
     ]
    }
   ],
   "source": [
    "def f(x, y):\n",
    "    if y == 0:\n",
    "        return np.nan\n",
    "    return x / y\n",
    "\n",
    "df = pd.DataFrame({\"a\": [10, 20, 30], \"b\": [40, 0, 60]})\n",
    "print(f(df[\"a\"], df[\"b\"]))  # ValueError"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0.25  nan 0.5 ]\n"
     ]
    }
   ],
   "source": [
    "def f(x, y):\n",
    "    if y == 0:\n",
    "        return np.nan\n",
    "    return x / y\n",
    "\n",
    "df = pd.DataFrame({\"a\": [10, 20, 30], \"b\": [40, 0, 60]})\n",
    "f_vec = np.vectorize(f)\n",
    "print(f_vec(df[\"a\"], df[\"b\"]))# [0.25  nan 0.5 ]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "                              sleep_quality\n",
      "sleep_duration stress_level                \n",
      "(0, 5]         (0.991, 3.25]       6.781818\n",
      "               (3.25, 5.5]         6.161538\n",
      "               (5.5, 7.75]         5.677778\n",
      "               (7.75, 10.0]        6.082353\n",
      "(5, 6]         (0.991, 3.25]       5.876923\n",
      "               (3.25, 5.5]         6.777778\n",
      "               (5.5, 7.75]         6.058333\n",
      "               (7.75, 10.0]        6.438462\n",
      "(6, 7]         (0.991, 3.25]       6.472727\n",
      "               (3.25, 5.5]         5.841667\n",
      "               (5.5, 7.75]         5.377778\n",
      "               (7.75, 10.0]        5.545455\n",
      "(7, 8]         (0.991, 3.25]       6.314286\n",
      "               (3.25, 5.5]         5.375000\n",
      "               (5.5, 7.75]         7.280000\n",
      "               (7.75, 10.0]        5.700000\n",
      "(8, 9]         (0.991, 3.25]       5.423077\n",
      "               (3.25, 5.5]         6.640000\n",
      "               (5.5, 7.75]         5.877778\n",
      "               (7.75, 10.0]        6.320000\n",
      "(9, 10]        (0.991, 3.25]       5.788235\n",
      "               (3.25, 5.5]         6.790000\n",
      "               (5.5, 7.75]         6.010000\n",
      "               (7.75, 10.0]        6.287500\n",
      "(10, 11]       (0.991, 3.25]       5.853333\n",
      "               (3.25, 5.5]         6.057143\n",
      "               (5.5, 7.75]         6.000000\n",
      "               (7.75, 10.0]        6.083333\n",
      "(11, 12]       (0.991, 3.25]       6.335294\n",
      "               (3.25, 5.5]         6.700000\n",
      "               (5.5, 7.75]         6.071429\n",
      "               (7.75, 10.0]        6.370588\n"
     ]
    }
   ],
   "source": [
    "df = pd.read_csv(\"data/sleep.csv\")\n",
    "sleep_duration_stage = pd.cut(df[\"sleep_duration\"], [0, 5, 6, 7, 8, 9, 10, 11, 12])# 对睡眠时间进行划分\n",
    "stress_level_stage = pd.cut(df[\"stress_level\"], 4)# 对压力等级进行划分\n",
    "print(df.pivot_table(values=\"sleep_quality\", index=[sleep_duration_stage, stress_level_stage], aggfunc=\"mean\"))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
